using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace Infrastructure
{
    public class Encryption
    {
        private static string encryptKey = "JYt+SqfoXKEHD+03SLWC+bW7vhGbLmNv";

        //默认密钥向量
        private static byte[] Keys = { 0x41, 0x72, 0x65, 0x79, 0x6F, 0x75, 0x6D, 0x79, 0x53, 0x6E, 0x6F, 0x77, 0x6D, 0x61, 0x6E, 0x3F };
        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="encryptString"></param>
        /// <returns></returns>
        public static string Encrypt(string encryptString)
        {
            if (string.IsNullOrEmpty(encryptString))
                return string.Empty;
            RijndaelManaged rijndaelProvider = new RijndaelManaged();
            rijndaelProvider.Key = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 32));
            rijndaelProvider.IV = Keys;
            ICryptoTransform rijndaelEncrypt = rijndaelProvider.CreateEncryptor();

            byte[] inputData = Encoding.UTF8.GetBytes(encryptString);
            byte[] encryptedData = rijndaelEncrypt.TransformFinalBlock(inputData, 0, inputData.Length);

            return Convert.ToBase64String(encryptedData);
        }
        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="decryptString"></param>
        /// <returns></returns>
        public static string Decrypt(string decryptString)
        {
            if (string.IsNullOrEmpty(decryptString))
                return string.Empty;
            try
            {
                RijndaelManaged rijndaelProvider = new RijndaelManaged();
                rijndaelProvider.Key = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 32));
                rijndaelProvider.IV = Keys;
                ICryptoTransform rijndaelDecrypt = rijndaelProvider.CreateDecryptor();

                byte[] inputData = Convert.FromBase64String(decryptString);
                byte[] decryptedData = rijndaelDecrypt.TransformFinalBlock(inputData, 0, inputData.Length);

                return Encoding.UTF8.GetString(decryptedData);
            }
            catch
            {
                return "";
            }
        }

        /// <summary>
        /// 字符串加密算法
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static string Encrypts(string text)
        {
            string key = "JYt+SqfoXKEHD+03SLWC+bW7vhGbLmNv";
            string iv = "/vQV9OVLrdQ=";
            SymmetricAlgorithm mCSP = new TripleDESCryptoServiceProvider();
            //指定加密的运算模式
            mCSP.Mode = System.Security.Cryptography.CipherMode.ECB;
            //指定加密算法的填充模式
            mCSP.Padding = System.Security.Cryptography.PaddingMode.PKCS7;

            ////重新生成键
            //mCSP.GenerateKey();
            ////重新生成标量
            //mCSP.GenerateIV();
            //key = Convert.ToBase64String(mCSP.Key);
            //iv = Convert.ToBase64String(mCSP.IV);
            byte[] byt = Encoding.UTF8.GetBytes(text);
            using (MemoryStream ms = new MemoryStream())
            {
                CryptoStream cs = new CryptoStream(ms, mCSP.CreateEncryptor(Convert.FromBase64String(key), Convert.FromBase64String(iv)), CryptoStreamMode.Write);
                cs.Write(byt, 0, byt.Length);
                cs.FlushFinalBlock();
                cs.Close();
                return Convert.ToBase64String(ms.ToArray());
            }
        }
        /// <summary>
        /// 字符串解密
        /// </summary>
        /// <param name="encryptText">被加密的文本</param>
        /// <returns>返回解密后的文本</returns>
        public static string Decrypts(string encryptText)
        {
            string key = "JYt+SqfoXKEHD+03SLWC+bW7vhGbLmNv";
            string iv = "/vQV9OVLrdQ=";
            SymmetricAlgorithm mCSP = new TripleDESCryptoServiceProvider();
            mCSP.Key = Convert.FromBase64String(key);
            mCSP.IV = Convert.FromBase64String(iv);
            //指定加密的运算模式
            mCSP.Mode = System.Security.Cryptography.CipherMode.ECB;
            //指定加密算法的填充模式
            mCSP.Padding = System.Security.Cryptography.PaddingMode.PKCS7;

            byte[] byt = Convert.FromBase64String(encryptText);
            using (MemoryStream ms = new MemoryStream())
            {
                CryptoStream cs = new CryptoStream(ms, mCSP.CreateDecryptor(mCSP.Key, mCSP.IV), CryptoStreamMode.Write);
                cs.Write(byt, 0, byt.Length);
                cs.FlushFinalBlock();
                cs.Close();
                return Encoding.UTF8.GetString(ms.ToArray());
            }
        }
    }
}
